<?php

class LetIdentity extends CUserIdentity
{
    public $admin = false;
    public $typeData = 'discuz';
    public $keyName = 'uid';
    public $table = 'let_forum_ucenter_members t';    
    public $messages = array('error'=>array(), 'success'=>array(), 'warning'=>array());
    
    private $_id;
    
	public function __construct($username, $password, $admin=false, $typeData='discuz')
	{
		if ($typeData == 'vbb') {
            $this->keyName = 'userid';
            $this->table = 'eplay_user';
		}
        
        $this->username = $username;
		$this->password = $password;
        $this->typeData = $typeData;
        $this->admin = $admin;
	}
    
    public function authenticate()
    {
        $user = $this->getDetails($this->username);        
        // Neu $user['let_active'] không tồn tại thì gọi hàm $user = $this->createUserExtra($this->id);
        if ($user['let_active'] == NULL) {
            $user = $this->createUserExtra($user['uid']);
        }
        
        if ($user == NULL) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
            $this->messages['error'][] = 'Khong ton tai Username';
        } else {            
            if ($this->encodePassword($this->password, $user['salt']) !== $user['password']) {
                $this->messages['error'][] = 'Sai password.';
            } else if ($user['let_active'] == 0) {
                $this->messages['error'][] = 'Tai khoan chua kich hoat.';
            } else if ($this->admin == true AND $user['let_group'] !== '8') {
                $this->messages['error'][] = 'Ban khong duoc quyen truy cap khu vuc nay.';
            } else {
                $this->_id = $user[$this->keyName];
                                
                $this->setState('group', $user['let_group']);
                $this->setState('email', $user['email']);
                $this->errorCode = self::ERROR_NONE;
                
                /**
                 * Các method khác theo em không cần viết lại, Yii đã hỗ trợ sẵn
                 * logout($uri='')                      => Yii::app()->user->logout();
                 *                                         $this->redirect(Yii::app()->homeUrl);
                 * 
                 * data($key='')                        => Yii::app()->user->keyName
                 * 
                 * check_logined($admin=FALSE)          => Yii::app()->user->isGuest
                 * check_logined($admin=TRUE)           => Yii::app()->user->checkAccess('roleName')  //RBAC
                 * 
                 * get_field($userid = 0, $field = '')  => Chưa biết dùng khi nào, ko có ý kiến
                 *                                         Nhưng nếu muốn lấy thông tin Member thì có thể dùng:
                 *                                         $this->setState('keyName', $user['keyName']);
                 *                                         Yii::app()->user->keyName
                 */
            }  
        }        
        return !$this->errorCode;
    }
    
    public function getDetails($username='', $userid=0)
    {
        $command = Yii::app()->db->createCommand();
        $command->select();
        $command->from($this->table);
        if ($userid !== 0) {
            $command->where("{$this->keyName}=:keyName", array(':keyName'=>$userid));
        } elseif ($username !== '') {
            $command->where('username=:username', array(':username'=>$username));
        }
        $command->leftJoin('let_kit_user_extra ue', "ue.id=t.{$this->keyName}");
        return $command->queryRow();
    }
    
    public function createUserExtra($userid=0)
    {
        // Tạo bản ghi ở bảng user_extra sau đó return cái dưới
        
        //return $this->getDetails('', $userid);
        
        Yii::app()->db->createCommand()->insert('let_kit_user_extra', array(
            'id'=>$userid,
            'let_group'=>Yii::app()->controller->getModule('account')->defaultLetGroup,
            'let_active'=>Yii::app()->controller->getModule('account')->defaultLetActive,
        ));
    }
    
	public static function encodePassword($password='', $salt='')
	{
        return md5(md5($password).$salt);
	}
    
    
    public function getId()
    {
        return $this->_id;
    }
}